<?php
/**
 * @file $HeadURL: imagecategorydefine.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2014-12-9 下午7:56:51 $
 * @brief 
 */
namespace haibao\cms\business;

use \haibao\cms\data\ImageCategoryDefine as CategoryData;
use \haibao\cms\model\data\ImageCategoryDefine as CategoryModelData;

class ImageCategoryDefine extends \haibao\cms\business\Base{
	
	/**
	 * @param \haibao\cms\model\data\ImageCategoryDefine
	 */
	public static function getCategoryByName($categoryName, $id = null){
		if(!$categoryName){
			throw new BusinessException('分类名不能为空');
		}
		$data = new CategoryData();
		return $data->getCategoryByName($categoryName, $id);
	}
	
	public static function searchCategoryByName($name){
	    $data = new CategoryData();
	    return $data->searchCategoryByName($name);
	}
	
	/**
	 * 查询图片分类树结构
	 * @param \haibao\cms\model\data\ImageCategoryDefine
	 * @return array
	 */
	public static function getCategoryTree(){
		$data = new CategoryData();
		return $data->getCategoryTree();
	}

	/**
	 * 查询所有分类
	 * @param \haibao\cms\model\data\ImageCategoryDefine
	 * @return array
	 */
	public static function getAllCategorys(){
		$data = new CategoryData();
		$allCategorys = $data->getCacheAllCategorys();
		if(empty($allCategorys)){
			$allCategorys = $data->getAllCategorysDict();
			$data->setCacheAllCategorys($allCategorys);
		}
		return $allCategorys;
	}
	
	/**
	 * @return array
	 */
	public static function getCategoryById($id){
		if(!$id){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryData();
		$model = $data->getCategoryById($id);
		if (!$model){
			throw new BusinessException('分类不存在', self::DATA_EMPTY);
		}
		
		return $model;
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageCategoryDefine $model
	 */
	public static function saveImageCategoryDefine($model){
		self::checkOperation(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_ADD);
		
		if(!isset($model->ParentId)){
			throw new BusinessException('请选择父级分类');
		}
		if(!$model->CategoryName){
			throw new BusinessException('分类名不能为空');
		}
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		$model->Status = CategoryModelData::STATUS_ENABLE;
		$model->OrderNo = self::getMaxOrderNo($model->ParentId);
		
		$data = new CategoryData();
		$data->addCategory($model);
		
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_ADD, $model->toArray(true,true));
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageCategoryDefine $model
	 */
	public static function editImageCategoryDefine($model){
		self::checkOperation(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_EDIT);
		if(!$model->Id){
			throw new BusinessException('参数错误');
		}
		if(!isset($model->ParentId)){
			throw new BusinessException('请选择父级分类');
		}
		if(!$model->CategoryName){
			throw new BusinessException('分类名不能为空');
		}
		$data = new CategoryData();
		$data->updateCategoryById($model);
		
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_EDIT, $model->toArray(true,true));
	}
	
	/**
	 * @param \haibao\cms\model\data\ImageCategoryDefine $model
	 */
	public static function deleteImageCategoryDefine($id){
		self::checkOperation(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_DELETE);
		
		if(!$id){
			throw new BusinessException("标签ID不能为空");
		}
		if(self::isExitstChildCategory($id)){
			throw new BusinessException('当前分类下有子分类不能删除');
		}
		
		$data = new CategoryData();
		$data->deleteImageCategoryDefineById($id);
		
		self::saveOperateHistory(self::FUNC_CODE_IMAGE_CATEGORY_DEFINE_DELETE, $id);
	}
	
	/**
	 * 根据分类id获取子分类id
	 * @param int $id
	 * @return array
	 */
	public static function getChildCategoryById($id,$arrCategoryDefine = null){
		$data = new CategoryData();
		return (array)$data->getChildCategoryById(intval($id),$arrCategoryDefine);
	}
	
	/**
	 * 是否存在子级分类
	 * @param int $id
	 */
	private static function isExitstChildCategory($id){
		$data = new \haibao\cms\data\ImageLibrary();
		$count = $data->isExitstChildCategory($id);
		return $count > 0 ? true : false;
	}
	
	private static function getMaxOrderNo($ParentId){
		$data = new CategoryData();
		$maxVal = $data->getMaxOrderId($ParentId);
		return $maxVal ? $maxVal+1 : CategoryModelData::ORDER_NO_START;
	}
	
	public static function getCategoryByNameAndParentId($categoryName, $parentId = 0){
		if(!$categoryName){
			throw new BusinessException('分类名不能为空');
		}
		$data = new CategoryData();
		return $data->getCategoryByNameAndParentId($categoryName, $parentId);
	}
}